home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13792 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: portal.gmu.edu!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: A weird thing about printf()
  5. Date: 10 Apr 96 10:53:11 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.829133591@rscernix>
  8. References: <4kflr2$5if@dewey.csun.edu>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=US-ASCII
  12. Content-Transfer-Encoding: 7bit
  13. X-Newsreader: NN version 6.5.0 #7 (NOV)
  14.  
  15. In <4kflr2$5if@dewey.csun.edu> kc44097@csun.edu (chen) writes:
  16. >  
  17. >  The program :
  18. >
  19. >#include <stdio.h>
  20. >
  21. >int answer;
  22. >main()
  23. >{
  24. >  answer=2+2;
  25. >  printf("The answer is %d\n");
  26. >  return 0;
  27. >}
  28. >
  29. >  In printf(),I lost "answer" in the end,but it works,and give me the
  30. >result 0.I wonder how the compiler handle this condition.
  31.  
  32. It simply sends to printf whatever arguments you are asking it to pass.
  33. There are compilers (e.g. gcc) that can detected your mistake (if asked
  34. to) by analyzing the format string and comparing it to the rest of the
  35. argument list.  There is no requirement for a compiler to perform this
  36. kind of testing, it's a quality of implementation issue.
  37.  
  38. >Also,is this allow by C?
  39.  
  40. C says that this mistake invokes "undefined behaviour" and no matter what
  41. the compiler does, it is the right thing, by definition.
  42.  
  43. >(by this, I mean is this a leagal usage in language itself,
  44. >or just a mistake cause by the compiler?)
  45.  
  46. It is not legal usage and it is not a mistake caused by the compiler,
  47. it is a mistake caused by the programmer.  Yeah, I know, it's so convenient
  48. to blame your mistakes on the language or the compiler :-)
  49.  
  50. >  Can someone give me some advice ? Please e-mail me :
  51. >                   kc44097@huey.csun.edu
  52.  
  53. Because your time is so much more precious than ours and you can't
  54. "waste" it by reading the answers in the place where you posted the
  55. question?
  56.  
  57. Dan
  58. --
  59. Dan Pop
  60. CERN, CN Division
  61. Email: danpop@mail.cern.ch 
  62. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  63.